<?php
require 'db.php';
if(isset($_POST['AddTuKhung'])){
		if (isset($_REQUEST["MaMH"])){
			$maMH = $_REQUEST["MaMH"];
			$nienKhoa = $_REQUEST["NienKhoa"];
			$MaMonHoc = explode (",", $maMH );
			foreach ( $MaMonHoc as $mamh ){
				if(!empty($mamh)){
					$sql = "INSERT INTO dbo_kehoachhoithao(MaMH,NienKhoa) VALUES ('" . $mamh . "','" . $nienKhoa . "')";
					$result = $db->exec( $sql );
				}
			}
 			echo 1;
			exit();

		}
	}
if(isset($_POST['type']))$type=$_POST['type'];
if($type=='ngoai'){
$sql2="select count(dbo_monhoc.MaMH) from dbo_monhoc";
}
$stmt2 = $db->prepare($sql2);
$stmt2->execute();

$row = $stmt2->fetch();

// tong so dong co trong CSDL
$totalRow = $row[0];

// so dong tren moi trang
$limit = 8;

// tinh tong so trang
$total_page = $totalRow==0?1:ceil($totalRow/$limit);


// lan dau goi trang nen trang hien tai = 1
$current_page = 1;

// neu gia tri trang moi duoc submit
if (isset($_REQUEST['page'])){
	$current_page = $_REQUEST['page'] ;
}

// so trang hien thi cho chon; vi du << 1 | 2 | 3 | 4 | 5 | >>
$nr_page = 5;

// doan hien tai
$segment = 1;

if (isset($_POST['segment'])){
	$segment = $_POST['segment'];
}


// neu doan chon ke tiep
if (isset($_POST['next'])){
	$segment += 1;
}

// neu chon doan truoc do
if (isset($_POST['previous'])){
	$segment -= 1;
}



$begin = $segment==1?1:($segment-1)*$nr_page+1;

$end = ($begin+$nr_page-1)<$total_page?$begin+$nr_page-1:$total_page;
$current_page = $current_page<$begin?$begin:$current_page;
// tinh vi tri bat dau select
$start = $current_page==1?0:($current_page-1)*$limit;
if($type=='ngoai'){
$sql1="select dbo_monhoc.MaMH, TenMH from dbo_monhoc LIMIT $start,$limit";	
}
$stmt1 = $db->prepare($sql1);
$stmt1->execute();
?>


<div id="khungdt">
<table  class="table table-bordered table-hover" style="font-size: 13px;">
	
			<thead>
			<tr class="info"><th>--</th><th>STT</th><th>Mã học phần</th><th>Tên học phần</th></tr></thead>
			<tbody>
	<?php $i=1;
		if($stmt1->execute()){
				while($r=$stmt1->fetch()){
		?>
				
				<tr>
					<td><?php echo "<input name='chk[]'  value='" . $r['MaMH'] . "' class='chk' type='checkbox'/>";?></td>
					<td><?php echo $i++;?></td>
					<td><?php echo $r['MaMH'];?></td>
					<td><?php echo $r['TenMH'];?></td>
		<?php }} ?>	
			</tbody>
			<tfoot>
		 		<tr>
			<td colspan="7">
					 
		 			
		 			<ul class="pagination">
		 				<li class="<?php echo $current_page==1?'disabled':'';?>" >
		 					<a href="#" id="previous" 
		 						data-segment="<?php echo $segment==1?1:($segment-1); ?>" 
		 						data-page="<?php echo $end-$nr_page;?>">&laquo;</a></li>
		 				 
		 				 <?php for($i=$begin;$i<=$end;$i++):?>
		 				 	<li class="<?php echo $i==$current_page?'active':'';?>"><a href="#" class="page_number" 
		 				 		data-segment="<?php echo $segment; ?>" 
		 				 		data-page="<?php echo $i;?>"><?php echo $i;?> </a></li>
		 				 <?php endfor;?>
		 				 
		 				<li class="<?php echo $end==$total_page?'disabled':'';?>">
		 					<a href="#" id="next" 
		 						data-segment="<?php echo $segment+1; ?>" 
		 						data-page="<?php echo $end+1 ?>">&raquo;</a></li>
		 			</ul> 
			</td>
		</tr>
		 	</tfoot>
			</table>
			<hr>
					<div class="form-group">
		<div class="col-sm-5"></div>
		<div class="col-sm-3"><button  class="form-control btn btn-primary btn-xs" id="AddTuKhung" value="show">
      					Thêm môn học</button></div>	
			
			</div>
			</div>